<template>
  <div>
    <a-select
        mode="true"
        style="width: 100%"
        placeholder="请选择印章"
        @deselect="deselect"
        v-model="ids"
        :open="open"
        :disabled="disabled"
        :getPopupContainer="(triggerNode)=>{ return triggerNode.parentNode}"
        @dropdownVisibleChange="selectIds">
      <a-select-option v-for="item in selectedObj" :key="item.id">{{ item.name }}</a-select-option>
    </a-select>
    <select-normal-seal-input-model2 ref="inputModal" :urlList="urlList" :multi="multi"
                                     @choseObj="choseObj"></select-normal-seal-input-model2>
    <select-erp-seal-certificate-file-manage-list :seallist="sealList2"
                                                  @cCopy="copyData"></select-erp-seal-certificate-file-manage-list>
  </div>
</template>

<script>
import SelectNormalSealInputModel2 from './modules/SelectNormalSealInputModel2'
import { getAction } from '@/api/manage'
import SelectErpSealCertificateFileManageList from './SelectErpSealCertificateFileManageList'

export default {
  name: 'SelectPostInput',
  components: {
    SelectNormalSealInputModel2,
    SelectErpSealCertificateFileManageList
  },
  props: {
    disabled: {
      type: Boolean,
      required: false,
      default: false
    },
    open: {
      type: Boolean,
      required: false,
      default: false
    },
    value: {
      type: String,
      required: false
    },
    multi: {
      type: Boolean,
      default: false,
      required: false
    },
    urlList: {
      type: String,
      // default: '/api/erp/files/employee/erpEmployeeFile/list2?auditStatus=2",
      default: '/api/erp/administration/seal/erpSealCertificateFileManage/list2',
      required: false
    },
    props: {
      type: Object,
      default: null,
    }
  },
  data() {
    return {
      sealList2: [],
      ids: [],
      selectedObj: [],
      deleteIds: [],
      list: [],
      objs: '',
      url: {
        // queryById: '/api/erp/files/employee/erpEmployeeFile/queryById",
        // queryByIds: '/api/erp/files/employee/erpEmployeeFile/queryByIds",
        queryById: '/api/erp/administration/seal/erpSealCertificationChangeRecord/queryByIds2',
        queryByIds: '/api/erp/administration/seal/erpSealCertificationChangeRecord/queryByIds2',
      },
    }
  },
  mounted() {
    this.initObj(this.value);
  },
  watch: {
    value(val) {
      this.initObj(val);
    },
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  methods: {
    copyData(data) {
      // this.$parent.copeData(data);
      this.$emit('ccCopy', data);
    },
    initObj(ids) {
      this.deleteIds = [];
      this.selectedObj = [];
      if (ids && ids.length > 0) {
        this.ids = ids.split(',');
        let url = '';
        let param = {};
        // if(this.ids.length==1){
        //   url=this.url.queryById;
        //   param={id:ids};
        // }else{
        url = this.url.queryByIds;
        param = { ids: ids };
        // }
        getAction(url, param).then((res) => {
          if (res.success) {
            if (!Array.isArray(res.result)) {
              this.selectedObj.push(res.result);
            } else {
              for (let i = 0; i < res.result.length; i++) {
                this.selectedObj.push(res.result[i]);
              }
            }
            this.$refs.inputModal.edit(res.result, this.ids);
            this.sealList2 = res.result;
          }
        });
      } else {
        this.ids = [];
        this.$refs.inputModal.edit(null, null);
      }

    },
    resetObj() {
      this.userType = false;
      this.ids = [];
      this.selectedObj = [];
      this.deleteIds = [];
      this.open = false;
      this.$refs.inputModal.edit(null, null);
    },
    selectIds() {
      if (!this.disabled) {
        this.deleteId();
        this.$refs.inputModal.add(this.selectedObj, this.ids);
      }
    },
    deselect(value, option) {
      this.deleteIds.push(value);
      this.deleteId();
      this.choseObj(this.selectedObj);
    },
    deleteId() {
      let that = this;
      this.selectedObj = this.selectedObj.filter(function (e) {
        return that.deleteIds.indexOf(e.id) == -1;
      })
      this.deleteIds = [];
    },
    // 子modal回调
    choseObj: function (list) {
      this.selectedObj = list;
      this.ids = [];
      for (var i = 0; i < list.length; i++) {
        this.ids.push(list[i].id);
      }
      this.objs = this.ids.join(',');
      this.$emit('change', this.objs);
      this.$emit('back', this.selectedObj);
    },
  }
}
</script>

<style scoped>

</style>